// pages/news/news.js
Page({

   /**
    * 页面的初始数据
    */
   data: {
      newsHeader: {
         headerImg: "http://s1.pstatp.com/growth/mobile_detail/image/banner_toutiao_icon.6NgPGihF.png",
         headerTitle: "新闻头条"
      },
      newsContent: [],
      length: 0
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
      this.getNews()
   },
   getNews: function() {
      var that = this
      let olddata = that.data.newsContent
      wx.showLoading({
         title: '正在加载'
      })
      wx.request({
         url: 'https://v.juhe.cn/toutiao/index?type=top&key=fde5671bfcca947dee8c5d09a9b5101c',
         success(res) {
            if (res.data.resultcode == 112) {
               var msg = res.reason 
               wx.showLoading({
                  title: '正在加载',
               })
               setTimeout(function() {
                     wx.hideLoading()
                     wx.showToast({
                        title: '超过每日可允许请求次数!',
                        icon: "none",
                        duration: 2000
                     })
                  },
                  2000)
            } else {
               console.log(res.data)
               var result = res.data.result.data
               var newdata = result.slice(that.data.length, that.data.newsContent.length + 10)
               console.log(that.data.newsContent)
               console.log(result.slice(that.data.length, that.data.newsContent.length + 10))
               if ((newdata.length - that.data.newsContent.length) == 10) {
                  that.setData({
                     newsContent: olddata.concat(newdata),
                     length: that.data.length
                  })
                  setTimeout(function() {
                     wx.hideLoading()
                  }, 1000)
               } else {
                  wx.hideLoading()
                  wx.showToast({
                     title: '已经到底了',
                     icon: 'none'
                  })
               }
            }
         }
      })
   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function() {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {
     wx.setNavigationBarColor({
       frontColor: '#ffffff',
       backgroundColor: '#F5B223'
     })
   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function() {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function() {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function() {
      this.getNews()
   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function() {
      this.getNews()
   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function() {

   }
})